<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

	/*
	 * 
	 */
use_helper('User');

	function renderFeedView(ActivityFeed $feed) {
		$feed_type = $feed->getType();		// get the feed type
		$render_func = 'render' . ucwords($feed_type);	// the method to render the feed
		return call_user_func($render_func, $feed);		// call the method
	}
	
	function renderNote(ActivityFeed $feed) {
		$user = new User();
		$course = new Course();
		$note = new Note();
		
		$user = UserPeer::retrieveById($feed->getUserId());
		$note = NotePeer::retrieveById($feed->getTypeId());
		$course = CoursePeer::retrieveById($note->getCourseId());
		if($user->getId() == sfContext::getInstance()->getUser()->getId())
			$who = "You";
		else
			$who = link_to("<strong>" . $user->getFullName() . "</strong>", 'user_profiles/show?id=' . $user->getId());
		$stored_message = $feed->getMessage();
		
		/*Until all note associations are sorted out*/
		$course_link = link_to("<span>" . $course->getName() . "</span>", 'courses/show?id=' . $course->getId(), array('class'=>'course-title'));
		$topic_link = link_to("<span>" . $note->getTopic() . "</span>", 'notes/show?id=' . $note->getId(), array('class'=>'subject'));
		$message = $who . ' ' . $stored_message . ' ' . $course_link . ':' . $topic_link;
		
		return "<li><p>" . $message . "</p></li>";
	}
	
	function renderComment(ActivityFeed $feed) {
		$user = new User();
		$comment = new Comment();
		
		$user = UserPeer::retrieveById($feed->getUserId());
		$comment = CommentPeer::retrieveByPK($feed->getTypeId());
		$owner = UserPeer::retrieveById($comment->getOwnerId());
		if($user->getId() == sfContext::getInstance()->getUser()->getId())
			$who = "You";
		else
			$who = link_to("<strong>" . $user->getFullName() . "</strong>", 'user_profiles/show?id=' . $user->getId());
		$stored_message = $feed->getMessage();
		if($owner->getId() == sfContext::getInstance()->getUser()->getId())	// does the current user own the object being commented on?
			$owner_link = "your";	// no link is necessary because they are the owner
		else if($owner->getId() == $user->getId())
			$owner_link = "their own";
		else {
			$owner_name = $owner->getFullName() . '\'s';
			$owner_link = link_to("<strong>" . $owner_name . "</strong>", 'user_profiles/show?id=' . $owner->getId());
		}

		$comment_type = $comment->getType();
		$obj_class = ucwords($comment_type) . 'Peer';
		$type_obj = call_user_func(array($obj_class, 'retrieveByPK'), $comment->getTypeId());
		$obj_link = link_to(strtolower($comment_type), strtolower($comment_type) . 's/show?id=' . $type_obj->getId());
		
		$message = $who . ' ' . $stored_message . ' ' . $owner_link  . ' ' . $obj_link;
		return "<li><p>" . $message . "</p></li>";
	}
	
	function renderTask(ActivityFeed $feed) {
		$user = new User();
		$task = new Task();
		$group = new Group();
		
		$user = UserPeer::retrieveById($feed->getUserId());
		$task = TaskPeer::retrieveById($feed->getTypeId());
		$group = GroupPeer::retrieveById($feed->getGroupId());
		if($user->getId() == sfContext::getInstance()->getUser()->getId())
			$who = "You";
		else
			$who = link_to("<strong>" . $user->getFullName() . "</strong>", 'user_profiles/show?id=' . $user->getId());
		$stored_message = $feed->getMessage();
		$group_link = link_to($group->getName(), 'groups/show?id=' . $group->getId());

		$message = $who . ' ' . $stored_message . ' ' . $group_link;
		
		return "<li><p>" . $message . "</p></li>";
	}
	
	function renderFile(ActivityFeed $feed) {
		$user = new User();
		$group = new Group();
			
		$user = UserPeer::retrieveById($feed->getUserId());
		$file = FilePeer::retrieveById($feed->getTypeId());	
		
		if($user->getId() == sfContext::getInstance()->getUser()->getId())
			$who = "You";
		else
			$who = link_to("<strong>" . $user->getFullName() . "</strong>", 'user_profiles/show?id=' . $user->getId());
		$stored_message = $feed->getMessage();
		
		if($feed->getGroupId())
		{
			$group = GroupPeer::retrieveById($feed->getGroupId());
			$group_link = link_to($group->getName(), 'groups/show?id=' . $group->getId());
			$message = $who . ' ' . $stored_message . ' for ' . $group_link;
		}
		else
		{
			$message = $who . ' ' . $stored_message.', <a href="'.$file->getAnnotateFileDownloadURL().'">'.$file->getName()."</a>";
		}
		
		return "<li><p>" . $message . "</p></li>";
	}
	
	function renderMessage(ActivityFeed $feed) {
		$user = new User();
		$message = new Message();
		$group = new Group();
		
		$user = UserPeer::retrieveById($feed->getUserId());
		$message = MessagePeer::retrieveById($feed->getTypeId());
		if($user->getId() == sfContext::getInstance()->getUser()->getId())
			$who = "You";
		else
			$who = link_to("<strong>" . $user->getFullName() . "</strong>", 'user_profiles/show?id=' . $user->getId());
		$stored_message = $feed->getMessage();
		
		if($feed->getGroupId())
		{
			$group = GroupPeer::retrieveById($feed->getGroupId());
			$group_link = link_to($group->getName(), 'groups/show?id=' . $group->getId());
			$activity_message = $who . ' ' . $stored_message . ' to '.$group_link;
			$message_link = link_to('Read the full post &#xBB;' , 'messages/index?group_id='.$group->getId());
		}
		else
		{
			$activity_message = $who . ' ' . $stored_message.' to '.render_profile_link($message->getSender());
			$message_link = '';
		}
		
		//crop body and add elipses
		$str = $message->getBody();
		$len = 100;
		if (strlen($str) > $len)
	    {
	    $str = substr($str, 0, $len);
	    $str = $str . "...";
	    }
		
		
		$feed_output = '<li class="gray"><p>
		'.$activity_message.'
		</p><blockquote><p>'.$str.'</p></blockquote>
		<p class="read-more">'.$message_link;
		
		return '';//$feed_output;

		
	}
	
	function renderSummary(ActivityFeed $feed) {
		$user = new User();
		$summary = new Summary();
		$group = new Group();
		
		$user = UserPeer::retrieveById($feed->getUserId());
		$summary = SummaryPeer::retrieveById($feed->getTypeId());
		if($user->getId() == sfContext::getInstance()->getUser()->getId())
			$who = "You";
		else
			$who = link_to("<strong>" . $user->getFullName() . "</strong>", 'user_profiles/show?id=' . $user->getId());
		
		$stored_message = $feed->getMessage();
		$activity_message = $who . ' ' . $stored_message.' '. link_to("<strong>". $summary->getTitle().' : '. $summary->getSubTitle()."</strong>", 'summaries/show?id='.$summary->getId());
			
		return "<li><p>".$activity_message."</li></p>";
	}
	
	function renderGroup(ActivityFeed $feed) {
		$user = new User();
		
		$user = UserPeer::retrieveById($feed->getUserId());
		if($user->getId() == sfContext::getInstance()->getUser()->getId())
			$who = "You";
		else
			$who = link_to("<strong>" . $user->getFullName() . "</strong>", 'user_profiles/show?id=' . $user->getId());
		$stored_message = $feed->getMessage();
		
		
		$group = GroupPeer::retrieveById($feed->getTypeId());
		$group_link = link_to($group->getName(), 'groups/show?id=' . $group->getId());
		$activity_message = $who . ' ' . $stored_message.' '.$group_link;
	
		
		return "<li><p>".$activity_message."</p></li>";	
	}
?>